package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.entity.Customer;
import com.codingfuture.erp_system.service.CustomerService;
import com.codingfuture.erp_system.util.CheckUtils;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/v2")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    private final String str = "";

    //分页查询/模糊查询
    @GetMapping("/supplier//listByPage")
    public Result findByAll(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "1") int rows, Customer customer) {
        Page<Customer> page1 = PageHelper.startPage(page, rows);
        List<Customer> customerList = customerService.findByAll(customer);
        PageData<Customer> data = new PageData<>(customerList, page1.getTotal());
        return Result.ok(data);
    }

    //增加
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/supplier//add")
    public Result insertCustomer(Customer customer) {
        String name = customer.getName();
        String address = customer.getAddress();
        String contact = customer.getContact();
        String tele = customer.getTele();
        String email = customer.getEmail();
        //校验客户名称/联系地址/联系人/联系电话/邮件地址不能为空
        if ((name == null && address == null && contact == null && tele == null && email == null)
                || Objects.equals(name, str)
                || Objects.equals(address, str)
                || Objects.equals(contact, str)
                || Objects.equals(tele, str)
                || Objects.equals(email, str)) {
            str.replace(" ", "");
            return Result.error(400, "请输入全部参数");
        }
        //校验客户名称合法性
        else if (CheckUtils.checkChinese(customer.getName())) {
            return Result.error(500, "供应商名称不是中文/过长");
        }

        //校验email格式
        else if (CheckUtils.checkEmail(customer.getEmail())) {
            return Result.error(500, "邮箱格式不正确");
        }
        //校验联系电话
        else if (CheckUtils.checkMobile(customer.getTele())) {
            return Result.error(500, "联系电话格式不对，并需要11位数字电话号码");
        }
        //校验联系人名字合法性
        else if (CheckUtils.checkChinese(customer.getContact())) {
            return Result.error(500, "联系人名称不是中文/过长");
        }

        //校验联系地址合法性
        else if (CheckUtils.checkChinese(customer.getAddress())) {
            return Result.error(500, "联系地址不是中文/过长");
        }
        // 校验客户名称不能重复
        else if (customerService.findByName(name) == null) {
            boolean r = customerService.insertCustomer(customer);
            return r ?Result.ok():Result.error(400, "错误");
        }
        return Result.error(400, "输入内容不能与已有内容重复");
    }

    //删除
    @DeleteMapping("/supplier/")
    public Result deleteCustomer(Integer id) {
        if (id == null) {
            return Result.error(400, "id为空");
        } else {
            boolean r = customerService.deleteCustomer(id);
            return r ? Result.ok() : Result.error(500, "服务器错误");
        }
    }

    //通过ID查询
    @GetMapping("/supplier//selectone")
    public Result updateCustomer(Integer id, HttpSession httpSession) {
        log.debug("hi-hello");
        httpSession.setAttribute("id", id);
        Customer customer = customerService.findById(id);
        return customer == null ? Result.error(500, "服务端错误") : Result.OK(customer);
    }

    //修改
    @PostMapping("//supplier//update")
    public Result updateCustomer(Customer customer, HttpSession httpSession) {
        Object id = httpSession.getAttribute("id");
        customer.setUuid((Integer) id);
        String name =customer.getName();
        String address = customer.getAddress();
        String contact = customer.getContact();
        String tele = customer.getTele();
        String email = customer.getEmail();
        // 校验客户名称/联系地址/联系人/联系电话/邮件地址不能为空
        if ((name == null && address == null && contact == null && tele == null && email == null)
                || Objects.equals(name, str)
                || Objects.equals(address, str)
                || Objects.equals(contact, str)
                || Objects.equals(tele, str)
                || Objects.equals(email, str)) {
            return Result.error(400, "不能为空请输入正确的名称");
        }
        //校验客户名称合法性
        else if (CheckUtils.checkChinese(customer.getName())) {
            return Result.error(500, "供应商名称不是中文/过长");
        }
        //校验email格式
        else if (CheckUtils.checkEmail(customer.getEmail())) {
            //boolean b1 = CheckUtils.checkEmail(supplier.getEmail());
            return Result.error(500, "邮箱格式不正确");
        }
        //校验联系电话
        else if (CheckUtils.checkMobile(customer.getTele())) {
            return Result.error(500, "联系电话格式不对，需要11位数字电话号码");
        }

        //校验联系人名字合法性
        else if (CheckUtils.checkChinese(customer.getContact())) {
            return Result.error(500, "联系人名称不是中文/过长");
        }
        //校验联系地址合法性
        else if (CheckUtils.checkChinese(customer.getAddress())) {
            return Result.error(500, "联系地址不是中文/过长");
        }
        // 校验客户名称不能重复
        else if (customerService.findByName(name) == null){
            boolean result = customerService.updateCustomer(customer);
            return result ? Result.ok():Result.error(500,"添加错误");
        }
        return Result.error(400,"输入内容不能与已有内容重复");
    }


}
